package com.yjn.blog.controller;

import com.yjn.blog.model.UserModel;
import com.yjn.blog.model.UserText;
import com.yjn.blog.model.http.BlogReq;
import com.yjn.blog.model.http.CursorPageReq;
import com.yjn.blog.model.http.CursorPageResp;
import com.yjn.blog.service.BlogService;
import com.yjn.blog.util.bean.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Classname: BlogController
 * Package: com.yjn.blog.controller
 * Description:
 *
 * @Author 余集楠
 * @Create 2024/7/16 0016 22:03
 * @Version 1.8
 */
@RestController
@RequestMapping(value="/api")
@Slf4j
public class BlogController {
    @Autowired
    BlogService blogService;

    //创建新文章
    @PostMapping(value="/posts")
    public BaseResponse create(@Valid @RequestBody BlogReq req){
        return BaseResponse.ok(blogService.create(req));
    }

    //获取某个用户的文章列表
    @RequestMapping(value="/posts/list")
    public BaseResponse<CursorPageResp> getList(@Valid @RequestBody int userId, CursorPageReq req){
        return BaseResponse.ok(blogService.getList(userId,req));
    }

    //获取单篇文章详情
    @RequestMapping(value="/postId")
    public BaseResponse<UserText> getPostId(@Valid @RequestBody int postId){
        return BaseResponse.ok(blogService.getPostId(postId));
    }

    //更新文章
    @PutMapping(value="/postId/update")
    public BaseResponse update(@Valid @RequestBody UserText userText){
        return BaseResponse.ok(blogService.update(userText));
    }

    //删除文章
    @DeleteMapping(value="/postId/delete")
    public BaseResponse delete(@Valid @RequestBody int postId){
        return BaseResponse.ok(blogService.delete(postId));
    }
}
